package com.xx.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class HelloServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        System.out.println("进行登陆");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println("username = " + username);
        System.out.println("password = " + password);
        String remeber = request.getParameter("remeber");
        System.out.println(remeber);
        //优先查看是否有Cookie
        Cookie[] cookies = request.getCookies();
        for(Cookie cookie:cookies){
            if("true".equals(cookie.getValue()) && "remeberMe".equals(cookie.getName())){
                System.out.println("记住账号成功,直接登陆");
                request.getRequestDispatcher("login.jsp").forward(request, response);
                return;
            }
        }
        //记住我的情况下,才会去创建Cookie
        if("on".equals(remeber)){
            if("admin".equals(username) && "admin".equals(password)) {
                Cookie cookie = new Cookie("remeberMe", "true");
                response.addCookie(cookie);
                request.getRequestDispatcher("login.jsp").forward(request, response);
                //response.sendRedirect("success.jsp");
            }else if("wuyanjie".equals(username) && "123456".equals(password)) {
                Cookie cookie = new Cookie("remeberMe", "true");
                response.addCookie(cookie);
                request.getRequestDispatcher("login.jsp").forward(request, response);
                //response.sendRedirect("success.jsp");
            }else{
                response.sendRedirect("loginfail.jsp");
            }
        }else if(null==remeber){
            System.out.println("记住我");
            if("admin".equals(username) && "admin".equals(password)) {
                request.getRequestDispatcher("login.jsp").forward(request, response);
                //response.sendRedirect("success.jsp");
            }else if("xiaxing".equals(username) && "123456".equals(password)) {
                request.getRequestDispatcher("login.jsp").forward(request, response);
                //response.sendRedirect("success.jsp");
            }else{
                response.sendRedirect("loginfail.jsp");
            }
        }
//        }else if("admin".equals(username) && "123456".equals(password)){
//            response.sendRedirect("success.jsp");
//        }
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}

